package gee

import (
	"log"
	"net/http"
	"strings"
)

type HandlerFunc func(*Context)
type router struct {
	handlers map[string]HandlerFunc
}

func NewRouter() *router {

	return &router{handlers: make(map[string]HandlerFunc)}
}

func getRouterKey(method string, path string) string {

	return strings.Join([]string{method, path}, ":")
}

func (r *router) AddRouter(method string, path string, handler HandlerFunc) {

	r.handlers[getRouterKey(method, path)] = handler
}

func (r *router) Handle(c *Context) {

	routerKey := getRouterKey(c.Method, c.Path)
	handler, ok := r.handlers[routerKey]
	if !ok {
		log.Printf("404: path not found - %s\n", c.Path)
		c.SetStatusCode(http.StatusNotFound)
		return
	}
	log.Printf("handle path - %s\n", c.Path)
	handler(c)
}
